package com.axend.lib_base.view.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.InputFilter
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomInputBinding
import com.axend.lib_base.engine.toast.toast
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.ext.toFloatOrDefault
import com.axend.lib_base.utils.inputfilter.DecimalDigitsInputFilter


/**
 * 自定义输入框
 */
class CustomInputDialog(
    context:Context,
    title:String,
    onOkListener: OnOkListener
) : Dialog(context, R.style.Common_CustomDialog){

    private var dialogBinding: BaseDialogCustomInputBinding


    init {
        dialogBinding = DataBindingUtil.inflate(layoutInflater, R.layout.base_dialog_custom_input, null, false)
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
        lp.width = dp2px(300f)
        lp.height= dp2px(150f)
        window.attributes = lp
        dialogBinding.tvTipsTitle.text = title
        dialogBinding.btCancel.setOnClickListener {
            dismiss()
        }
        dialogBinding.btOk.setOnClickListener {
            var tempValue=dialogBinding.etInput.text?.trim()
            if (tempValue.isNullOrEmpty()){
//                toast(getContext().getString(R.string.app_tips_number_null))
                return@setOnClickListener
            }
            onOkListener?.onClick(this, tempValue.toString().toFloatOrDefault(0f))
        }
    }

    fun setInputValueFilter(inputFilter:InputFilter): CustomInputDialog{
        dialogBinding.etInput.filters=arrayOf(inputFilter)
        return this
    }

    interface OnOkListener{
        fun onClick(dialog: DialogInterface, value: Float)
    }
}